﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YZQ_DotNetArx.CAD
{
    /// <summary>
    /// 辅助操作类
    /// </summary>
    public static class Env
    {
        /// <summary>
        /// 当前的数据库
        /// </summary>
        public static Database Database => HostApplicationServices.WorkingDatabase;
        /// <summary>
        /// 当前文档
        /// </summary>
        public static Document Document => Application.DocumentManager.MdiActiveDocument;
        /// <summary>
        /// 编辑器对象
        /// </summary>
        public static Editor Editor => Document.Editor;
        /// <summary>
        /// 命令行打印,会自动调用对象的ToString函数
        /// </summary>
        /// <param name="message">要打印的对象</param>
        public static void Print(object message) => Editor.WriteMessage($"{message}\n");
        /// <summary>
        /// 命令行打印,会自动调用对象的ToString函数，在打印内容前加换行
        /// </summary>
        /// <param name="message"></param>
        public static void Print1(object message) => Editor.WriteMessage($"{Environment.NewLine}{message}\n");
        /// <summary>
        /// 获取当前.NET程序所在的目录
        /// </summary>
        /// <returns>获取当前.NET程序所在的目录</returns>
        public static string GetCurrentPath()
        {
            var moudle = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0];
            return System.IO.Path.GetDirectoryName(moudle.FullyQualifiedName);
        }
        /// <summary>
        /// 将实体添加到模型空间
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="ents">实体对象</param>
        /// <returns>返回添加到模型空间的实体的ObjectId</returns>
        public static ObjectIdCollection AddEntityToModelSpace<T>(List<T> ents) where T : Entity
        {
            ObjectIdCollection ids = new ObjectIdCollection();//用于返回添加到模型空间的实体
            using (Transaction trans = Database.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(Database.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                foreach (Entity ent in ents)
                {
                    ent.SetDatabaseDefaults();
                    btr.AppendEntity(ent);
                    trans.AddNewlyCreatedDBObject(ent, true);
                    ids.Add(ent.Id);
                }
                trans.Commit();
            }
            return ids;
        }
        /// <summary>
        /// 将实体添加到模型空间
        /// </summary>
        /// <param name="ents">实体对象</param>
        /// <returns>返回添加到模型空间的实体的ObjectId</returns>
        public static ObjectIdCollection AddEntityToModelSpace(params Entity[] ents)
        {
            ObjectIdCollection ids = new ObjectIdCollection();
            ents.ToList().ForEach(ent =>
            {
                AddEntityToModelSpace(new List<Entity> { ent });
                ids.Add(ent.Id);
            });
            return ids;
        }    
    }
}
